// 用户 的仓库数据
import {state} from "../api/exception";
import {stringify} from "qs";
import {message} from "../utils/message";
import {http} from "../api/axios";

let api = require("@/api").default;


export default {
    namespaced: true,
    state: {
        isLoading: false,
        searchParams: '',
    },
    getters:{

    },
    mutations:{
        /**
         * 设置加载状态
         */
        setIsLoading(state, payload){
            state.isLoading = payload;
        },
        /**
         * 设置加载状态
         */
        setSearchParams(state, payload){
            state.searchParams = payload;
        },

    },
    actions: {
        /**
         * 保存文章
         */
        async saveArticle(context, payload){
            context.commit("setIsLoading", true);

            let isSuccess = false;
            let resp = await api.article.saveArticle(payload);
            if(resp.code === state.Success){
                message.success("文章发布成功");
                isSuccess = true;
            }else{
                message.error(resp.message);
            }

            context.commit("setIsLoading", false);

            return isSuccess;
        },
        /**
         * 获取文章总数
         */
        async countTotal(context, payload){
            context.commit("setIsLoading", true);
            let total = 0;
            let resp = await api.article.countTotal(payload);
            if(resp.code === state.Success){
                total = resp.data;
            }else{
                message.error(resp.message);
            }

            context.commit("setIsLoading", false);

            return total;
        },
        /**
         * 获取文章概要
         */
        async listSimple(context, payload){
            context.commit("setIsLoading", true);
            let data = null;
            let resp = await api.article.listSimple(payload);
            if(resp.code === state.Success){
                data = resp.data;
                for (let key in data) {
                    data[key]["avatar"] = http + data[key]["avatar"];
                }
            }else{
                message.error(resp.message);
            }

            context.commit("setIsLoading", false);

            return data;
        },
        /**
         * 获取某个文章
         */
        async getArticle(context, payload){
            context.commit("setIsLoading", true);
            payload.host = http;
            let data = null;
            let resp = await api.article.getArticle(payload);
            if(resp.code === state.Success){
                data = resp.data;
            }else{
                message.error(resp.message);
            }
            context.commit("setIsLoading", false);
            return data;
        },
        /**
         * 删除某个文章
         */
        async deleteArticle(context, payload){
            context.commit("setIsLoading", true);
            let data = false;
            let resp = await api.article.deleteArticle(payload);
            if(resp.code === state.Success){
                data = true;
                message.success("删除成功");
            }else{
                message.error(resp.message);
            }
            context.commit("setIsLoading", false);
            return data;
        },
        /**
         * 上传文章图片
         * @return 图片地址
         */
        async uploadArticleImg(context, payload){
            context.commit("setIsLoading", true);
            let data = null;
            let resp = await api.article.uploadArticleImg(payload);
            if(resp.code === state.Success){
                data = http + resp.data;
                message.success("图片上传成功");
            }else{
                message.error(resp.message);
            }
            context.commit("setIsLoading", false);
            return data;
        },
        /**
         * 获取图片存放文件夹
         */
        async getUploadDirNum(context, payload){
            context.commit("setIsLoading", true);
            let data = null;
            let resp = await api.article.getUploadDirNum();
            if(resp.code === state.Success){
                data = resp.data;
            }else{
                message.error(resp.message);
            }
            context.commit("setIsLoading", false);
            return data;
        },
        /**
         * 获取下载文章的URL
         */
        async downloadUrl(context){
            return http + await api.article.download();
        },
        /**
         * 获取下载文章的URL
         */
        async search(context, payload){
            context.commit("setSearchParams", payload);
        }
    }
};